<template>
	<view class="mine">
		<image class="bgimg" src="../../static/image/minebg.png" mode=""></image>
		<view class="innerBox">

			<view class="minebgBox">
				<view class="title">
					我的
				</view>
				<view class="userInfo">
					<view v-if="showFlag" class="">
						<button class="loginBtn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
							<image v-if="pageData.avatar" :src="pageData.avatar"
								style="width: 160rpx; height: 160rpx;border-radius: 160rpx;">
							</image>
							<image v-else
								src="https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
								style="width: 160rpx; height: 160rpx;border-radius: 160rpx;margin-right: 30rpx;margin-left: 30rpx;">
							</image>
						</button>

					</view>

					<image v-else
						src="https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
						style="width: 160rpx; height: 160rpx;border-radius: 160rpx;margin-right: 30rpx;margin-left: 30rpx;">
					</image>
					<!-- 	<image :src="pageData.avatar" mode=""
					style="width: 120rpx;height: 120rpx;border-radius: 120rpx;"></image> -->
					<view class="messageInfo">
						<view class="topBox">
							<view class="nickName">
								{{pageData.name==null ||  pageData.name== "" ? '微信用户' : pageData.name}}
							</view>
							<view v-if="pageData.identity==2" class="type">
								顾问
							</view>
							<view v-if="pageData.identity==1" class="type" style="background: #27D8A6;">
								家长
							</view>
							<view v-if="pageData.identity==0" class="type" style="background: #879BFF;">
								学生
							</view>
						</view>
						<view v-if="pageData.identity==0" class="bottonBox">
							<view class="value">
								{{pageData.school == null ? '' : pageData.school}}
							</view>
							<view class="value" style="margin-left: 20rpx;">
								{{pageData.age == null ? '' : pageData.age}}
							</view>
						</view>

					</view>
				</view>
			</view>

			<view class="typeListBox">
				<view class="outBox">

					<view class="typeItem" @click="handleStudentfile">
						<image src="../../static/image/xsda.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view class="text">
							学生档案
						</view>
					</view>
					<view class="typeItem" @click="handleMymission">
						<image src="../../static/image/wdrw.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view v-if="showFlag">
							<view v-if="pageData.identity==0" class="text">
								我的任务
							</view>
							<view v-if="pageData.identity==1" class="text">
								孩子任务
							</view>
							<view v-if="pageData.identity==2" class="text">
								学生任务
							</view>
						</view>
						<view v-else class="text">
							我的任务
						</view>
					</view>
				</view>
				<view class="outBox">
					<view class="typeItem" @click="handleMeeting">
						<image src="../../static/image/wdhy.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view v-if="showFlag">
							<view v-if="pageData.identity==0" class="text">
								我的会议
							</view>
							<view v-if="pageData.identity==1" class="text">
								孩子会议
							</view>
							<view v-if="pageData.identity==2" class="text">
								学生会议
							</view>
						</view>
						<view v-else class="text">
							我的会议
						</view>
					</view>
					<view class="typeItem" @click="handleAcademic">
						<image src="../../static/image/xscj.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view class="text">
							学术成绩
						</view>
					</view>
				</view>
				<view class="outBox">
					<view class="typeItem" @click="handleExtracuurricular">
						<image src="../../static/image/kwhd.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view class="text">
							课外活动
						</view>
					</view>
					<view class="typeItem" @click="handlegotoMyApplications">
						<image src="../../static/image/wdsq.png" mode="" style="width: 60rpx;height: 60rpx;"></image>
						<view v-if="showFlag">
							<view v-if="pageData.identity==0" class="text">
								我的申请
							</view>
							<view v-if="pageData.identity==1" class="text">
								孩子申请
							</view>
							<view v-if="pageData.identity==2" class="text">
								学生申请
							</view>
						</view>

						<view v-else class="text">
							我的申请
						</view>
					</view>
				</view>
			</view>


			<view v-if="showFlag" class="exitBtn" @click="show = true">
				退出登录
			</view>
			<view v-if="showFlag" class="edit" @click="handleEditPassword">
				修改密码
			</view>

		</view>

		<u-modal :show="show" :title="title" :showCancelButton="true" @confirm="confirm" @cancel="cancel"></u-modal>

	</view>
</template>

<script>
	import {
		userinfo,
		editAvatar
	} from "@/api/api.js"
	export default {
		data() {
			return {
				show: false,
				title: "您确定要退出登录吗？",
				pageData: {},
				showFlag: false,
			}
		},
		onLoad() {

		},
		onShow() {
			if (uni.getStorageSync('token')) {
				this.showFlag = true
				this.handleuserinfo()
			} else {
				this.showFlag = false
			}
		},
		methods: {
			async handleuserinfo() {
				let res = await userinfo()
				// console.log("用户信息", res)
				// if (res.data.email == null || res.data.email == '') {
				// 	uni.showToast({
				// 		title: '请先绑定邮箱！',
				// 		icon: "none",
				// 		duration: 2500
				// 	});
				// 	setTimeout(() => {
				// 		uni.reLaunch({
				// 			url: "/pages/login/bindMailbox"
				// 		})
				// 	}, 2700)

				// 	return
				// }
				this.pageData = res.data
			},
			// 获取头像临时路径
			onChooseAvatar(e) {
				let that = this
				let avatatImg = e.detail.avatarUrl
				uni.uploadFile({
					url: 'https://lycheedu.com.cn/api/index/upload',
					filePath: avatatImg,
					name: 'file',
					formData: {
						'user': 'test'
					},
					success: async (uploadFileRes) => {

						// this.avatar = e.detail.avatarUrl
						// console.log("成功回调", JSON.parse(uploadFileRes));						
						// console.log("成功回调", JSON.parse(uploadFileRes.data).data);
						avatatImg = JSON.parse(uploadFileRes.data).data
						// console.log("avatatImg", avatatImg)
						let res = await editAvatar({
							avatar: avatatImg
						})
						if (res.code == 1000) {
							// console.log("更新头像")
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 2000
							});
							this.handleuserinfo()
						} else {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 2000
							});
						}
					},
					fail: (e) => {
						// console.log("上传失败", e)
					}
				});
			},
			//判断是否绑定有限
			handleBindEmail() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
			},

			//学生列表
			handleStudentfile() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `/pages/mine/archivesMessage?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}
			},

			//任务
			handleMymission() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `./myMission?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}

			},

			//成绩
			handleAcademic() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `./academicAchievement?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}

			},

			//课外活动
			handleExtracuurricular() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `./extracurricularActivities?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}

			},

			//申请
			handlegotoMyApplications() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `./myApplication?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}

			},

			//会议列表
			handleMeeting() {
				if (!uni.getStorageSync('token')) {
					uni.showToast({
						title: "请先登录后查看！",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/bindMailbox"
						});
					}, 1500)
					return
				}
				if (this.pageData.identity == 0) {
					uni.navigateTo({
						url: `./meetingMinutes?id=${this.pageData.id}`
					})
				} else {
					uni.navigateTo({
						url: "/pages/mine/studentFile"
					})
				}

			},
			confirm() {
				uni.removeStorageSync('token')
				this.show = false
				uni.reLaunch({
					url: "/pages/login/bindMailbox"
				})
			},
			cancel() {
				this.show = false
			},
			handleEditPassword() {
				uni.navigateTo({
					url: "/pages/login/index"
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.edit {
		width: 100%;
		text-align: center;
		color: #255A9B;
		font-size: 30rpx;
		margin-top: 20rpx;
	}

	.mine {
		width: 100%;
		height: 100%;
		background-color: #ffffff;
		position: relative;

		.bgimg {
			width: 100%;
			height: 100%;
		}

		.innerBox {
			position: absolute;
			top: 0;
			left: 0;
			margin-left: 30rpx;
		}

		.minebgBox {
			padding-top: 110rpx;
			box-sizing: border-box;

			// background-image: url('http://10.0.0.126:10030/uploads/images/Frame 2@2x.png');

			.title {
				font-size: 31rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				color: #000000;
				line-height: 37rpx;
				width: 100%;
				text-align: center;
			}

			.userInfo {
				width: 690rpx;
				height: 180rpx;
				background: #255A9B;
				border-radius: 32rpx 32rpx 0rpx 0rpx;
				margin: 0 auto;
				margin-top: 58rpx;
				padding: 30rpx 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;

				.loginBtn {
					// width: 160rpx !important;
					height: 160rpx !important;
					background-color: transparent;
					// background-color: pink;
					border: none !important;
					outline: none !important;
					margin: 0 0 !important;
				}

				.loginBtn::after {
					border: none;
				}

				.messageInfo {
					// margin-left: 20rpx;

					.topBox {
						display: flex;
						align-items: center;

						.nickName {
							font-size: 36rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							color: #FFFFFF;
							line-height: 42rpx;
						}

						.type {
							width: 64rpx;
							height: 30rpx;
							background: #F23D4F;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							font-size: 24rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							color: #FFFFFF;
							line-height: 30rpx;
							text-align: center;
							margin-left: 20rpx;
						}
					}

					.bottonBox {
						display: flex;
						font-size: 28rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: rgba(255, 255, 255, 0.6);
						line-height: 33rpx;
						margin-top: 16rpx;
					}
				}
			}

		}

		.typeListBox {
			width: 100%;
			margin-top: 138rpx;

			.outBox {
				width: 100%;
				display: flex;
				justify-content: space-around;
				text-align: center;
				margin-bottom: 106rpx;

				.typeItem {
					width: 112rpx;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #255A9B;
					line-height: 33rpx;

					.text {
						margin-top: 20rpx;
					}
				}
			}

		}

		.exitBtn {
			width: 630rpx;
			height: 88rpx;
			background: #EEEEEE;
			border-radius: 200rpx 200rpx 200rpx 200rpx;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #999999;
			line-height: 88rpx;
			text-align: center;
			margin: 0 auto;
			margin-top: 138rpx;
		}
	}
</style>